// ============================================================================
//
// LCP-net framework by Pierre CHATEL chatelp@gmail.com
// Distributed under GPLv3
// Copyright (C) THALES Land & Joint Systems, 2008, 2009. All rights reserved
//
// ============================================================================

/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package com.thalesgroup.preferenceFramework.models.impl;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList;
import org.eclipse.emf.ecore.util.EObjectWithInverseResolvingEList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.InternalEList;

import com.thalesgroup.preferenceFramework.models.Arc;
import com.thalesgroup.preferenceFramework.models.ModelsPackage;
import com.thalesgroup.preferenceFramework.models.Node;
import com.thalesgroup.preferenceFramework.models.NodeValue;
import com.thalesgroup.preferenceFramework.models.PreferenceNetwork;

/**
 * <!-- begin-user-doc --> An implementation of the model object '
 * <em><b>Node</b></em>'. <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.impl.NodeImpl#getName <em>Name</em>}</li>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.impl.NodeImpl#getInArcs <em>In Arcs</em>}</li>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.impl.NodeImpl#getOutArcs <em>Out Arcs</em>}</li>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.impl.NodeImpl#getDomain <em>Domain</em>}</li>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.impl.NodeImpl#getPreferenceNetwork <em>Preference Network</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public abstract class NodeImpl extends EObjectImpl implements Node {
	/**
	 * The default value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected static final String NAME_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected String name = NAME_EDEFAULT;

	/**
	 * The cached value of the '{@link #getInArcs() <em>In Arcs</em>}' reference list.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @see #getInArcs()
	 * @generated
	 * @ordered
	 */
	protected EList<Arc> inArcs;

	/**
	 * The cached value of the '{@link #getOutArcs() <em>Out Arcs</em>}' reference list.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @see #getOutArcs()
	 * @generated
	 * @ordered
	 */
	protected EList<Arc> outArcs;

	/**
	 * The cached value of the '{@link #getDomain() <em>Domain</em>}' containment reference list.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @see #getDomain()
	 * @generated
	 * @ordered
	 */
	protected EList<NodeValue> domain;

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	protected NodeImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return ModelsPackage.Literals.NODE;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Arc> getInArcs() {
		if (inArcs == null) {
			inArcs = new EObjectWithInverseResolvingEList<Arc>(Arc.class, this, ModelsPackage.NODE__IN_ARCS, ModelsPackage.ARC__END_NODE);
		}
		return inArcs;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Arc> getOutArcs() {
		if (outArcs == null) {
			outArcs = new EObjectWithInverseResolvingEList<Arc>(Arc.class, this, ModelsPackage.NODE__OUT_ARCS, ModelsPackage.ARC__START_NODE);
		}
		return outArcs;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public String getName() {
		return name;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public void setName(String newName) {
		String oldName = name;
		name = newName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ModelsPackage.NODE__NAME, oldName, name));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public EList<NodeValue> getDomain() {
		if (domain == null) {
			domain = new EObjectContainmentWithInverseEList<NodeValue>(NodeValue.class, this, ModelsPackage.NODE__DOMAIN, ModelsPackage.NODE_VALUE__NODE);
		}
		return domain;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public PreferenceNetwork getPreferenceNetwork() {
		if (eContainerFeatureID != ModelsPackage.NODE__PREFERENCE_NETWORK) return null;
		return (PreferenceNetwork)eContainer();
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetPreferenceNetwork(
			PreferenceNetwork newPreferenceNetwork, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newPreferenceNetwork, ModelsPackage.NODE__PREFERENCE_NETWORK, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public void setPreferenceNetwork(PreferenceNetwork newPreferenceNetwork) {
		if (newPreferenceNetwork != eInternalContainer() || (eContainerFeatureID != ModelsPackage.NODE__PREFERENCE_NETWORK && newPreferenceNetwork != null)) {
			if (EcoreUtil.isAncestor(this, newPreferenceNetwork))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newPreferenceNetwork != null)
				msgs = ((InternalEObject)newPreferenceNetwork).eInverseAdd(this, ModelsPackage.PREFERENCE_NETWORK__NODES, PreferenceNetwork.class, msgs);
			msgs = basicSetPreferenceNetwork(newPreferenceNetwork, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ModelsPackage.NODE__PREFERENCE_NETWORK, newPreferenceNetwork, newPreferenceNetwork));
	}

	public EList<Node> parentNodes() {
		EList<Arc> inArcs = this.getInArcs();
		BasicEList<Node> parentNodes = new BasicEList<Node>();
		for (Arc arc : inArcs) {
			Node parentNode = arc.getStartNode();
			parentNodes.add(parentNode);
		}
		return parentNodes;
	}

	public EList<Node> childNodes() {
		EList<Arc> outArcs = this.getOutArcs();
		BasicEList<Node> childNodes = new BasicEList<Node>();
		for (Arc arc : outArcs) {
			Node childNode = arc.getEndNode();
			childNodes.add(childNode);
		}
		return childNodes;
	}

	public EList<Node> inputNodes() {
		return this.parentNodes();
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd,
			int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ModelsPackage.NODE__IN_ARCS:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getInArcs()).basicAdd(otherEnd, msgs);
			case ModelsPackage.NODE__OUT_ARCS:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getOutArcs()).basicAdd(otherEnd, msgs);
			case ModelsPackage.NODE__DOMAIN:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getDomain()).basicAdd(otherEnd, msgs);
			case ModelsPackage.NODE__PREFERENCE_NETWORK:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetPreferenceNetwork((PreferenceNetwork)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd,
			int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ModelsPackage.NODE__IN_ARCS:
				return ((InternalEList<?>)getInArcs()).basicRemove(otherEnd, msgs);
			case ModelsPackage.NODE__OUT_ARCS:
				return ((InternalEList<?>)getOutArcs()).basicRemove(otherEnd, msgs);
			case ModelsPackage.NODE__DOMAIN:
				return ((InternalEList<?>)getDomain()).basicRemove(otherEnd, msgs);
			case ModelsPackage.NODE__PREFERENCE_NETWORK:
				return basicSetPreferenceNetwork(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(
			NotificationChain msgs) {
		switch (eContainerFeatureID) {
			case ModelsPackage.NODE__PREFERENCE_NETWORK:
				return eInternalContainer().eInverseRemove(this, ModelsPackage.PREFERENCE_NETWORK__NODES, PreferenceNetwork.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case ModelsPackage.NODE__NAME:
				return getName();
			case ModelsPackage.NODE__IN_ARCS:
				return getInArcs();
			case ModelsPackage.NODE__OUT_ARCS:
				return getOutArcs();
			case ModelsPackage.NODE__DOMAIN:
				return getDomain();
			case ModelsPackage.NODE__PREFERENCE_NETWORK:
				return getPreferenceNetwork();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case ModelsPackage.NODE__NAME:
				setName((String)newValue);
				return;
			case ModelsPackage.NODE__IN_ARCS:
				getInArcs().clear();
				getInArcs().addAll((Collection<? extends Arc>)newValue);
				return;
			case ModelsPackage.NODE__OUT_ARCS:
				getOutArcs().clear();
				getOutArcs().addAll((Collection<? extends Arc>)newValue);
				return;
			case ModelsPackage.NODE__DOMAIN:
				getDomain().clear();
				getDomain().addAll((Collection<? extends NodeValue>)newValue);
				return;
			case ModelsPackage.NODE__PREFERENCE_NETWORK:
				setPreferenceNetwork((PreferenceNetwork)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case ModelsPackage.NODE__NAME:
				setName(NAME_EDEFAULT);
				return;
			case ModelsPackage.NODE__IN_ARCS:
				getInArcs().clear();
				return;
			case ModelsPackage.NODE__OUT_ARCS:
				getOutArcs().clear();
				return;
			case ModelsPackage.NODE__DOMAIN:
				getDomain().clear();
				return;
			case ModelsPackage.NODE__PREFERENCE_NETWORK:
				setPreferenceNetwork((PreferenceNetwork)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case ModelsPackage.NODE__NAME:
				return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT.equals(name);
			case ModelsPackage.NODE__IN_ARCS:
				return inArcs != null && !inArcs.isEmpty();
			case ModelsPackage.NODE__OUT_ARCS:
				return outArcs != null && !outArcs.isEmpty();
			case ModelsPackage.NODE__DOMAIN:
				return domain != null && !domain.isEmpty();
			case ModelsPackage.NODE__PREFERENCE_NETWORK:
				return getPreferenceNetwork() != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (name: ");
		result.append(name);
		result.append(')');
		return result.toString();
	}

} // NodeImpl
